//  Copyright © 2018 Sunface <CTO@188.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package g

import (
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var L *zap.Logger

func InitLogger() {
	level := zap.DebugLevel

	atom := zap.NewAtomicLevel()

	// To keep the example deterministic, disable timestamps in the output.
	encoderCfg := zap.NewProductionEncoderConfig()
	encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
	L = zap.New(zapcore.NewCore(
		zapcore.NewConsoleEncoder(encoderCfg),

		zapcore.Lock(os.Stdout),
		atom,
	), zap.AddCaller())

	atom.SetLevel(level)
}

func Debug(debugOn bool, msg string, fields ...zapcore.Field) {
	if debugOn || Conf.Common.LogLevel == "debug" {
		L.Debug(msg, fields...)
	}
}

func Info(msg string, fields ...zapcore.Field) {
	L.Info(msg, fields...)
}
func Warn(msg string, fields ...zapcore.Field) {
	L.Warn(msg, fields...)
}

func Error(msg string, fields ...zapcore.Field) {
	L.Error(msg, fields...)
}

func Fatal(msg string, fields ...zapcore.Field) {
	L.Fatal(msg, fields...)
}
